var progect_type=null;
var mold_type=null;
var work_type=null;
var voltage_type=null;
$(function () {
    $.when(getdatadictionary("progect_type"), getdatadictionary("mold_type")
        , getdatadictionary("work_type"), getdatadictionary("voltage_type")).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'api/bbudgetitem/list?aguid=' + GetUrlParam("aguid"),
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                {label: '所属预算标准ID', name: 'bGuid', index: 'b_guid', width: 80, hidden: true},
                {
                    label: '项目类别',
                    name: 'bIType',
                    index: 'b_i_type',
                    width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in progect_type) {
                            if (cellvalue == progect_type[d].dKey) return progect_type[d].dValue;
                        }
                    }
                },
                {label: '预算项目分类', name: 'bIClass', index: 'b_i_class', width: 80},
                {label: '预算项目', name: 'bIItem', index: 'b_i_item', width: 80},
                {
                    label: '类型',
                    name: 'bIMold',
                    index: 'b_i_mold',
                    width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in mold_type) {
                            if (cellvalue == mold_type[d].dKey) return mold_type[d].dValue;
                        }
                    }
                },
                {
                    label: '工作类容',
                    name: 'bIWork',
                    index: 'b_i_work',
                    width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in work_type) {
                            if (cellvalue == work_type[d].dKey) return work_type[d].dValue;
                        }
                    }
                },
                {
                    label: '电压等级',
                    name: 'bIVoltage',
                    index: 'b_i_voltage',
                    width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in voltage_type) {
                            if (cellvalue == voltage_type[d].dKey) return voltage_type[d].dValue;
                        }
                        return "";
                    }
                },
                {label: '预算参数', name: 'bIParameter', index: 'b_i_parameter', width: 80},
                {label: '计价单位', name: 'bIUnit', index: 'b_i_unit', width: 80},
                {label: '取值下限', name: 'bILower', index: 'b_i_lower', width: 80},
                {label: '取值上限', name: 'bIHigh', index: 'b_i_high', width: 80},
                {label: '价格', name: 'bIPrice', index: 'b_i_price', width: 80},
                {label: '取值方式', name: 'bIValue', index: 'b_i_value', width: 80},
                {label: '说明', name: 'bIExplain', index: 'b_i_explain', width: 80}
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    );

});

function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            progect_type = r.returnlist;
        }
         if (sss == "mold_type") {
            mold_type = r.returnlist;
        }
         if (sss == "work_type") {
            work_type = r.returnlist;
        }
         if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        }
       vm.progect_type=progect_type;
        vm.work_type=work_type;
    });
}

var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            bIType: null,
            bIWork: null,
            bIItem:null,
            bIClass: null
        },
        showList: true,
        title: null,
        bBudgetItem: {},
        progect_type: {},
        mold_type: {},
        work_type: {},
        voltage_type: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bBudgetItem = {};
            vm.progect_type = progect_type;
            vm.mold_type = mold_type;
            vm.work_type = work_type;
            vm.voltage_type = voltage_type;
            vm.bBudgetItem.bGuid=GetUrlParam("aguid");


        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.progect_type = progect_type;
            vm.mold_type = mold_type;
            vm.work_type = work_type;
            vm.voltage_type = voltage_type;
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bBudgetItem.guid == null ? "api/bbudgetitem/save" : "api/bbudgetitem/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bBudgetItem),
                success: function (r) {
                    if (r.code === 0) {
                        if(vm.bBudgetItem.guid == null)
                        {

                            confirmaa('操作成功，是否继续添加？', function (r) {

                                if(r=="ok")
                                {

                                    vm.add();
                                }
                                if(r=="no")
                                {
                                    vm.reload();
                                }
                            });

                        }
                        else
                        {
                            alert('操作成功', function (index) {

                                vm.reload();
                            });
                        }
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bbudgetitem/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bbudgetitem/info/" + guid, function (r) {
                vm.bBudgetItem = r.bBudgetItem;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{
                    'bIType': vm.q.bIType,
                    'bIWork': vm.q.bIWork,
                    'bIItem': vm.q.bIItem,
                    'bIClass': vm.q.bIClass

                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});